
"use strict"
var call = require("../../utils/request.js")
const app = getApp()
Page({
  data: {
    deposit:0,
    bprice:0,
    allprice:0,
    allmoney:0,
    kexuan:true,
    bukexuan:false,
    ordernum:'',
    price:'',
    yuetype:'',
    cardtype:'',
    yue:'',
    ruzhutype:'',
    select:'wechat',
    hours:'0',
    phone:'',
    nickName: '',
  },

  onLoad: function(e) {

    if (wx.getStorageSync("uid")) {
      this.setData({
        ordernum: e.ordernum,
        price: e.price,
        ruzhutype:e.ruzhutype
      })

      var call = require("../../utils/request.js")
      var url = 'paytype?uid=' + wx.getStorageSync("uid")+'&paymoney='+e.price+'&ordernum='+e.ordernum;
      call.getData(url, this.paytype, this.fail)
      
      var url = 'getuserphone?uid=' + wx.getStorageSync("uid");
      call.getData(url, this.getuserphone, this.fail)

    } else {

      setTimeout(() => {
        wx.showToast({
          title: '请先授权登录',
          icon: "none",
        });
        setTimeout(() => {
          wx.switchTab({
            url: '../mine/mine'
          })
        }, 2000)
      }, 0);
    }

  },
  
  onSelect: function (data) {
    console.log(data.currentTarget.dataset.id)
    this.setData({
      select:data.currentTarget.dataset.id,
    })

  },

  
  getuserphone: function (data) {
  
    if(data.msg == 'success'){
      this.setData({
        phone: data.data.phone,
      })
      if(data.data.nickName != 'undefined')
   {  this.setData({
    nickName: data.data.nickName,
  })}

    }

  },
  paytype: function (data) {
  
    this.setData({
      allmoney:data.data.allmoney,
      deposit:data.data.deposit,
      bprice:data.data.bprice,
      allprice:data.data.allprice,
      cardtype: data.data.card,
      yuetype: data.data.money,
      yue: data.data.yue,
      hours:data.data.cardyue,
    })
    if(this.data.ruzhutype == 'quanri'){
      this.setData({
        cardtype: 0
      })
    }
 
  },


  gohome: function (data) {
    wx.reLaunch({
      url: '../index/index',
    })
  },

  chongzhi: function (data) {
    wx.reLaunch({
      url: '../pageck/recharge',
    })
  },

  getnickname: function (data) {

    this.setData({
      nickName: data.detail.value,

    })
  },


  getphone: function (data) {

    this.setData({
      phone: data.detail.value,

    })
  },


   // 微信支付付
   pay: function pay() {
      if(!this.data.nickName || !this.data.phone){
        wx.showToast({
          title:'请输入正确的个人信息',
          icon: 'none',
          duration: 3000
        })
        return false;
      }
    // if(this.guoqi == 0){
      wx.request({
        url: "https://www.yunzhuke.net/api/data/getPrePay",
        data: {
          openid: wx.getStorageSync("openid"),
          ordernum: this.data.ordernum,
          price: this.data.price,
          paytype:this.data.select,
          ordername:this.data.nickName,
          orderphone:this.data.phone
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
            console.log(9222222)
  
          if (res.data == 234) {
            wx.showToast({
              title: '下单失败,请取消订单重新支付',
              icon: 'none',
              duration: 3000
            })
          } else if (res.data == 456){
            wx.showToast({
              title:'订单已过期,请取消订单后重新下单',
              icon: 'none',
              duration: 3000
            })
          }else if (res.data.code == 0){
            wx.showToast({
              title:res.data.msg,
              icon: 'none',
              duration: 3000
            })
          }else {
            let prepay_id = res.data.data.prepay_id  //预支付id
            let order_no = res.data.data.order_no    //订单号
            let timeStamp = String(res.data.data.timeStamp)  //服务器端返回的时间戳，必须强转字符串，微信要求的
            let nonceStr = res.data.data.nonce_str //服务器返回的随机字符串
            // let package = 'prepay_id=' + res.data.prepay_id //package需要这么填写，自己注意
            let packageres = 'prepay_id=' + res.data.data.prepay_id
            let paySign = res.data.data.paySign //支付签名

            // 拉起微信支付
            wx.requestPayment({
              'timeStamp': timeStamp,
              'nonceStr': nonceStr,
              'package': packageres,
              'signType': 'MD5',
              'paySign': paySign,
              success: function (res) {
                //接口调用成功的回调函数
                console.log('支付success')
                console.log(res.errMsg)
                if (res.errMsg == 'requestPayment:ok'){
                  setTimeout(() => {
                    wx.showToast({
                      title: '支付成功',
                      icon: "none",
                    });
                    setTimeout(() => {
                      wx.reLaunch({
                        url: '../refund/refund?ordernum=' + order_no
                      })
                    }, 2000)
                  }, 0);
                }
             

              },
              fail: function (res) {
                console.log(8)
                console.log(res)
                wx.showToast({
                  title: '支付失败',
                  icon: 'none',
                  duration: 3000
                })
                //接口调用失败的回调函数
              },
              complete: function (res) {
                console.log(9)
                console.log(res)
                //接口调用结束的回调函数（调用成功、失败都会执行）
              }
            })
          }

        }
      })
    // }else{
    //   //wmc
    //   setTimeout(() => {
    //     wx.showToast({
    //       title: '订单已过期,请取消订单后重新下单',
    //       icon: "none",
    //     });
    //   }, 0);
    // }
  
  }




})